package com.yaozheng.vocationaltraining.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 文件操作
 *
 * @author Administrator
 */
public class FileUtils {
    /**
     * 缓冲大小
     */
    public static final int BUFFER_SIZE = 1024;

    /**
     * 删除文件
     *
     * @param file
     */
    public static void deleteFile(File file) {
        try {
            if (file != null && file.exists()) {
                file.delete();
            }
        } catch (Exception ex) {
            ErrorUtils.outErrorLog(ex);
        }
    }

    /**
     * 复制文件
     *
     * @param inputStream
     * @param outputStream
     * @return
     */
    public static boolean copyFile(InputStream inputStream, File outputStream) {
        try {
            return copyFile(inputStream, new FileOutputStream(outputStream));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            ErrorUtils.outErrorLog(e);
            return false;
        }
    }

    /**
     * 复制文件
     *
     * @param inputStream
     * @param outputStream
     * @return true 成功 false 不成功
     */
    public static boolean copyFile(InputStream inputStream, OutputStream outputStream) {
        try {
            if (inputStream == null)
                return false;
            if (outputStream == null)
                return false;
            byte[] bbs = new byte[BUFFER_SIZE];
            int b;
            while ((b = inputStream.read(bbs, 0, bbs.length)) != -1) {
                outputStream.write(bbs, 0, b);
            }
            return true;
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            // throw e;
            return false;
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception e) {
                ErrorUtils.outErrorLog(e);
                return false;
            }
        }
    }
}
